using Microsoft.EntityFrameworkCore;
using ProxyServer.EntityFrameworkCore.Npgsql;
using ProxyServer.EntityFrameworkCore.Sqlite;

namespace ProxyServer.EntityFrameworkCore;

public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddEntityFrameworkCore(this IServiceCollection services, string? providerName, string? connectionString)
    {
        ArgumentNullException.ThrowIfNullOrWhiteSpace(connectionString);

        switch (providerName)
        {
            case DatabaseProviderNames.Npgsql:
                services.AddSingleton<ModelBuilderOptions<AppDbContext>>(new ModelBuilderOptions<AppDbContext>(NpgsqlModelBuilder.ConfigureModel));
                break;
            default:
                services.AddSingleton<ModelBuilderOptions<AppDbContext>>(new ModelBuilderOptions<AppDbContext>(SqliteModelBuilder.ConfigureModel));
                break;
        }

        services.AddDbContext<AppDbContext>(options =>
        {
            switch (providerName)
            {
                case DatabaseProviderNames.Npgsql:
                    options.UseNpgsql(connectionString);
                    break;
                default:
                    options.UseSqlite(connectionString);
                    break;
            }
        });

        return services;
    }
}